package dao.impl;

import dao.UserDao;
import domain.User;
import utils.JdbcUtil;

import java.sql.*;

/*
 * 用户Dao实现类
 */
public class UserDaoImpl implements UserDao {
    String message;

    public String Back() {
        return message;
    }

    public User login(String username, String password) {

        User user = null;
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {

            conn = JdbcUtil.getConnection();
            String sql = "select * from user where username = ? and password= ?";
            PreparedStatement prpstmt = null;
            prpstmt = conn.prepareStatement(sql);
            prpstmt.setString(1, username);
            prpstmt.setString(2, password);
            rs = prpstmt.executeQuery();
            if (rs.next()) {
                user = new User();
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setEmail(rs.getString("email"));
                user.setUnit(rs.getString("unit"));
                user.setRegistertime(rs.getString("registertime"));
                user.setRealname(rs.getString("realname"));
                user.setPhone(rs.getString("phone"));
                user.setBirth(rs.getString("birth"));
                message = "登录成功";
            } else {
                message = "用户名或者密码错误";
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.release(rs, stmt, conn);
        }
        return user;
    }

    public User register(User user) {

        Connection conn = null;
        PreparedStatement prpstmt = null;
        ResultSet rs = null;
        @SuppressWarnings("unused")
        int result = 0;

        try {
            conn = JdbcUtil.getConnection();
            String sql = "insert into user(username, password, email, unit, registertime) values (?, ?, ?, ?, ?);";
            prpstmt = conn.prepareStatement(sql);
            prpstmt.setString(1, user.getUsername());
            prpstmt.setString(2, user.getPassword());
            prpstmt.setString(3, user.getEmail());
            prpstmt.setString(4, user.getUnit());
            prpstmt.setString(5, user.getRegistertime());

            result = prpstmt.executeUpdate();


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.release(rs, prpstmt, conn);
        }
        if (result != 0) {
            message = "注册成功";
        } else {
            message = "注册失败";
        }

        return user;
    }

    public boolean select(String username, String email) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql_1 = "select count(*) from user where username = ?";
        String sql_2 = "select count(*) from user where email = ?";
        int count = 0;
        try {
            conn = JdbcUtil.getConnection();
            ps = conn.prepareStatement(sql_1);
            ps.setString(1, username);
            rs = ps.executeQuery();
            rs.next();

            count = rs.getInt(1);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.release(rs, ps, conn);
        }

        try {
            conn = JdbcUtil.getConnection();
            ps = conn.prepareStatement(sql_2);
            ps.setString(1, email);
            rs = ps.executeQuery();
            rs.next();

            count += rs.getInt(1);
            if (count > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.release(rs, ps, conn);
        }
        return false;
    }


    public void updateUser(User user) {
        Connection conn = null;
        PreparedStatement ps = null;
        String sql = "update user set password=? where username=?";
        int rs = 0;

        try {
            conn = JdbcUtil.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setString(1, user.getPassword());
            ps.setString(2, user.getUsername());
            rs = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.release(null, ps, conn);
        }
        if (rs != 0) {
            message = "修改密码成功";
        } else {
            message = "修改密码失败";
        }
    }


    @Override
    public void addInfo(User user) {
        Connection conn = null;
        PreparedStatement ps = null;
        String sql = "update user set realname=?, phone=?, birth=? where username=?";
        @SuppressWarnings("unused")
        int rs = 0;

        try {
            conn = JdbcUtil.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setString(1, user.getRealname());
            ps.setString(2, user.getPhone());
            ps.setString(3, user.getBirth());
            ps.setString(4, user.getUsername());
            rs = ps.executeUpdate();
            message = "添加信息成功";
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.release(null, ps, conn);
        }

    }
}